import { CircleModelMonitorComp } from 'db://app-game/libs/ecs/common/model/CircleModelMonitorComp';
import BTConditional from 'db://app-game/libs/math/behavior-tree/base/BTConditional';
import { NodeStatus } from 'db://app-game/libs/math/behavior-tree/enum';
import { Monster } from '../../ecs/biz/monster/Monster';
import { MonitorModelAttackComp } from '../../ecs/biz/monster/model/MonitorModelAttackComp';

/**
 * 圆形监控范围
 *
 * 1. 创建圆形监控范围
 * 2. 获取碰撞列表
 * 3. 判断碰撞列表是否为空
 * 4. 如果碰撞列表不为空，则返回成功
 * 5. 如果碰撞列表为空，则返回失败
 */
export default class ConditionalMonsterCircleMonitor extends BTConditional {
  onUpdate(monster: Monster): NodeStatus {
    // 1. 如果怪兽有锁定攻击目标，则返回成功
    if (monster.get(MonitorModelAttackComp)) return NodeStatus.Success;

    // 2. 获取碰撞列表
    const cmm = monster.get(CircleModelMonitorComp);
    const colliderList = cmm.colliderList;

    // 3. 判断碰撞列表是否为空
    if (colliderList.length > 0) {
      // 4. 如果碰撞列表不为空，则锁定攻击目标
      monster.lockAttackTarget(colliderList[0].data.node);
      return NodeStatus.Success;
    }

    // 5. 如果碰撞列表为空，则返回失败
    return NodeStatus.Failure;
  }
}
